--
-- Author: mengjie
-- Date: 2016-04-14 13:42:41
--
module(...,package.seeall)
basePath = "image/sound/"
local SoundLoadTable = {}
local userDefault = cc.UserDefault:getInstance()
local _isEffectOn = userDefault:getIntegerForKey("isEffectOn", 1)==1
local _isBackgroundOn = userDefault:getIntegerForKey("isBackgroundOn", 1)==1
local sharedEngine = cc.SimpleAudioEngine:getInstance()
local backgroundMusic=""
local PianoMusics = require("script.PianoMusics")


--------------------------------------------background sound-------------------------------------------------
function setBackgroundMusic(bOn)
    if bOn then
        _isBackgroundOn = bOn
        if backgroundMusic and backgroundMusic~="" then
            playBackgroundMusic(backgroundMusic,true)
        end
        
    else
       
        -- pauseBackgroundMusic()
        if backgroundMusic and backgroundMusic~="" then
            stopBackgroundMusic(backgroundMusic)
        end
    end
    _isBackgroundOn = bOn
    userDefault:flush()
end

function isBackgroundOn()
    return _isBackgroundOn --sharedEngine:isBackgroundOn()
end

--    static void end()
--function preloadBackgroundMusic(pszFilePath)
--	sharedEngine:preloadBackgroundMusic(pszFilePath)
--end

function playBackgroundMusic(pszFilePath,bLoop)
    --	if bLoop == nil then
    --		bLoop = false
    --	end
    backgroundMusic = pszFilePath
    if isBackgroundOn() and pszFilePath and pszFilePath~="" then
        sharedEngine:stopMusic(true)
        sharedEngine:playMusic(pszFilePath,bLoop)
        setBackgroundMusicVolume(0.3)
    end
end

function stopBackgroundMusic(bReleaseData)
    --	if bReleaseData == nil then
    --		bReleaseData = false
    --	end
    sharedEngine:stopMusic()

end

function pauseBackgroundMusic()
    if isBackgroundOn() then
        sharedEngine:pauseMusic()
    end
end

function resumeBackgroundMusic()
    if isBackgroundOn() then
        sharedEngine:resumeMusic()
    end
end

function rewindBackgroundMusic()
    if isBackgroundOn() then
        sharedEngine:rewindBackgroundMusic()
    end
end

function willPlayBackgroundMusic()
    return sharedEngine:willPlayBackgroundMusic()
end

function isBackgroundMusicPlaying()
    return sharedEngine:isBackgroundMusicPlaying()
end

function getBackgroundMusicVolume()
    return sharedEngine:getBackgroundMusicVolume()
end

function setBackgroundMusicVolume(volume)
    sharedEngine:setMusicVolume(volume)
end

--------------------------------------------effect sound-----------------------------------------------------
function setEffectMusic(bOn)
    if bOn then
        userDefault:setIntegerForKey("isEffectOn", 1)
    else
        userDefault:setIntegerForKey("isEffectOn", 0)
    end
    _isEffectOn = bOn
    userDefault:flush()
end

function isEffectOn()
    return _isEffectOn --sharedEngine:isEffectOn()
end

function getEffectsVolume()
    return sharedEngine:getEffectsVolume()
end

function setEffectsVolume(volume)
    sharedEngine:setEffectsVolume(volume)
end

function playEffect(pszFilePath,bLoop)
    --	if bLoop == nil then
    --		bLoop = false
    --	end
    if isEffectOn()  and pszFilePath and pszFilePath~=""  then
        if not SoundLoadTable[pszFilePath] then
            SoundLoadTable[pszFilePath]=true
        end
        return sharedEngine:playEffect(pszFilePath,bLoop)
    end
end

function pauseEffect(nSoundId)
    if isEffectOn() then
        sharedEngine:pauseEffect(nSoundId)
    end
end

function pauseAllEffects()
    if isEffectOn() then
        sharedEngine:pauseAllEffects()
    end
end

function resumeEffect(nSoundId)
    if isEffectOn() then
        sharedEngine:resumeEffect(nSoundId)
    end
end

function resumeAllEffects()
    if isEffectOn() then
        sharedEngine:resumeAllEffects()
    end
end

function stopEffect(nSoundId)
    if isEffectOn() then
        sharedEngine:stopEffect(nSoundId)
    end
end

function stopAllEffects()
    if isEffectOn() then
        sharedEngine:stopAllEffects()
    end
end

function preloadEffect(pszFilePath)
    if not SoundLoadTable[pszFilePath] then
        SoundLoadTable[pszFilePath] = true
        sharedEngine:preloadEffect(pszFilePath)
    end
end

function unloadEffect(pszFilePath)
    if SoundLoadTable[pszFilePath] then
        sharedEngine:unloadEffect(pszFilePath)
        SoundLoadTable[pszFilePath]= nil
    end
end

function unloadAllEffect()
    local l_sound = sharedEngine
    for k,v in pairs(SoundLoadTable) do
        if v then
            l_sound:unloadEffect(k)
            SoundLoadTable[v]=false
        end
    end
end
local index = 1
function reset()
    index = 1
end

function playClick()
    local soundid = PianoMusics.KISS_THE_RAIN[index]
    index=index+1
    if index>#PianoMusics.KISS_THE_RAIN then
        index = 1
    end
    soundid="image/sounds/piano/sound"..index..".m4a"
    playEffect(soundid)
end
function playError()
    playEffect("image/sounds/error.m4a")
end